//
//  HOQuestionReplyViewController
//  WeiDu_1_0
//
//  Created by foguo-mac-1 on 01/07/16.
//  Copyright (c) 2016 WeiDu Energy Tech. All rights reserved.
//

#import "HOQuestionReplyViewController.h"
#import "Logger.h"
#import "HOUtils.h"
#import "HOConstants.h"
#import "HOEditView.h"
#import "HODefine.h"

@implementation HOQuestionReplyViewController

- (id)initWithModule:(HO_MODULE_TYPE)type
{
	return [super initWithModule:type];
}

- (void)sendNow:(HOEditView *)endView
{
}
//@Override
- (void)viewDidLoad
{
	[super viewDidLoad];
	self.inputBoxHeight = 80;
	
	inputBox = [[HOEditView alloc]
				initWithFrame:CGRectMake(0,
										 20,
										 self.view.frame.size.width,
										 self.inputBoxHeight)];
	oneLineView = [[UIView alloc]
				   initWithFrame:CGRectMake(-1,
											20,
											self.view.frame.size.width + 2,
											self.inputBoxHeight)];
	[oneLineView setBackgroundColor:[UIColor whiteColor]];
	oneLineView.layer.borderWidth  = 1;
	oneLineView.layer.borderColor = UIColorFromRGB(0xcdcdcd).CGColor;
	oneLineInputBox = [[UITextField alloc]
					   initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
												0,
												self.view.frame.size.width - HO_HORIZONAL_PADDING * 2,
												self.inputBoxHeight)];
	
	oneLineInputBox.textColor = UIColorFromRGB(0x666000);
	oneLineInputBox.clearButtonMode = UITextFieldViewModeWhileEditing;
	oneLineInputBox.font = [UIFont systemFontOfSize:15];
	oneLineInputBox.delegate = self;
	inputBox.placeHolder = NSLocalizedString(@"请输入回复内容", @"请输入回复内容");
	//    oneLineInputBox.layer.borderWidth = 1;
	inputBox.limitedCount = 200;
	if(self.needHideLimitedLbl)
		[inputBox hideLimitedCountView];
	[self.view addSubview:inputBox];
	[oneLineView addSubview:oneLineInputBox];
	[self.view addSubview:oneLineView];
	[inputBox getTextView].font = [UIFont systemFontOfSize:15];
	
	
	
	
	inputBox.hidden = NO;
	[[inputBox getTextView] becomeFirstResponder];
	oneLineView.userInteractionEnabled = NO;
	oneLineView.hidden = YES;
	
	[self registerTapGesture];
	
}

- (void)hideKeyboard
{
	[inputBox resignFirstResponder];
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
	//	if([self.fieldName isEqualToString:kHO_FIELD_TelPhone])
	//	{
	//		NSString *s = [textField.text stringByReplacingCharactersInRange:range withString:string];
	//		NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"^[0-9-]+$" options:0 error:nil];
	//		NSTextCheckingResult *match = [regex firstMatchInString:s options:0 range:NSMakeRange(0, [s length])];
	//		return (match != nil);
	//	}
	//	else
	//	{
	//		return YES;
	//	}
	return YES;
}
- (void)hideLimitedLbl
{
	
	[inputBox hideLimitedCountView];
}

//@Override
- (void)viewWillAppear:(BOOL)animated
{
	self.navigationController.navigationBar.hidden = NO;
	self.tabBarController.tabBar.hidden = YES;
	[self setNavigationItemTitle:NSLocalizedString(@"提问", @"提问")];
	[super viewWillAppear:animated];
	[super registerKeyboard];
	
	[self displayRightButtomItemWithTitle:NSLocalizedString(@"提交", @"提交")];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
	[super unregisterKeyboard];
}

- (void)hideHintView
{
	[super hideHintView];
}

- (void)submitContent
{
	
	//	NSString *title = [titleLbl.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
	
	NSString *content = [[inputBox getText] stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
	if(content == nil || content.length == 0)
	{
		[self popupHintView:NSLocalizedString(@"请填写问题内容", @"请填写问题内容")];
		return;
	}
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_AddReply,
								  kHO_FIELD_QuestionId:[NSNumber numberWithInt:self.questionEntity.questionId],
								  kHO_FIELD_QuestionContent:content}];
	
}

//@Override
- (void)rightButtonItemDidClick
{
	[self submitContent];
}

//@Override
- (void)didReceiveResponse:(HOBaseResponseEntity *)response
{
	if(![response.action isEqualToString:kHO_ACTION_AddReply])
		return;
	[super didReceiveResponse:response];
	if([response isRequestDone])
	{
		if([response.action isEqualToString:kHO_ACTION_AddReply])
		{
			[self popupHintView:NSLocalizedString(@"修改成功!", @"修改成功!") inTime:1 needCallback:YES];
			self.view.userInteractionEnabled = NO;
		}
	}
	
}


#pragma mark
//@Override
- (void)hintDidHide
{
	self.view.userInteractionEnabled = YES;
	[self goBack];
}
//@Override
- (void)dealloc
{
	[HOUtils removeFromSuperViewForViews:oneLineInputBox, inputBox];
	[oneLineInputBox release];
	[inputBox release];
	
	[super dealloc];
}
@end
